package com.koron.ui;

import java.awt.Component;
import java.awt.Container;
import java.lang.reflect.InvocationTargetException;

import javax.swing.*;
import javax.swing.text.JTextComponent;

import org.apache.commons.beanutils.BeanUtils;

public class POJOToFrame {
	Container container;

	public POJOToFrame(Container container) {
		this.container = container;
	}

	/**
	 * 获取容器中的组件，根据组件的名称，返回指定的POJO对象
	 * 
	 * @param container 容器组件
	 * @param clazz POJO对象的类
	 * @return
	 */
	public final <T> T toPOJO(Class<T> clazz) {
		try {
			T pojo = clazz.newInstance();
			int size = container.getComponentCount();
			for (int i = 0; i < size; i++) {
				Component c = container.getComponent(i);
				String name = c.getName();
				if ((name == null || name.isEmpty()) && !(c instanceof JScrollPane) && !(c instanceof JPanel))
					continue;
				getValue(c, pojo);
			}
			return pojo;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}

	private void getValue(Component c, Object pojo) {
		Object value = null;
		String name = c.getName();
		if (c instanceof JTextComponent)
			value = ((JTextComponent) c).getText();
		else if (c instanceof JComboBox)
			value = ((JComboBox<?>) c).getSelectedItem();
		else if (c instanceof JScrollPane) {
			getValue(((JScrollPane) c).getViewport().getComponent(0), pojo);
			return;
		}else if (c instanceof JPanel) {
			for (Component com : ((JPanel)c).getComponents()) {
				getValue(com, pojo);
			}
			return;
		}
		try {
			if(name!=null && !name.isEmpty())
			BeanUtils.setProperty(pojo, name, value);
		} catch (InvocationTargetException | IllegalAccessException e) {
		}
	}

	public final void fromPOJO(Object pojo) {
		int size = container.getComponentCount();
		for (int i = 0; i < size; i++) {
			Component c = container.getComponent(i);
			String name = c.getName();
			setValue(pojo, c);
		}
	}

	private void setValue(Object pojo, Component c) {
		String name = c.getName();
		if ((name == null || name.isEmpty()) && !(c instanceof JScrollPane) && !(c instanceof JPanel))
			return;
		try {
			if (c instanceof JTextComponent) {
				Object value = BeanUtils.getProperty(pojo, name);
				((JTextComponent) c).setText(value.toString());
			} else if (c instanceof JComboBox) {
				Object value = BeanUtils.getProperty(pojo, name);
				ComboBoxModel<?> model = ((JComboBox<?>) c).getModel();
				for (int j = 0; j < model.getSize(); j++) {
					if (compareString(model.getElementAt(j), value)) {
						model.setSelectedItem(j);
						break;
					}
				}
			} else if (c instanceof JScrollPane) {
				setValue(pojo,((JScrollPane) c).getViewport().getComponent(0));
			}else if (c instanceof JPanel) {
				for (Component com : ((JPanel)c).getComponents()) {
					setValue(pojo, com);
				}
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 比较对象和字符串,缺省调用toString方法
	 * 
	 * @param o
	 * @param s
	 * @return
	 */
	public boolean compareString(Object o, Object s) {
		if (o.toString().equals(s))
			return true;
		return false;
	}
}